package pri.ydy.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import pri.ydy.commonUtils.JwtUtils;
import pri.ydy.commonUtils.MD5;
import pri.ydy.educenter.entity.UcenterMember;
import pri.ydy.educenter.entity.vo.RegisterVo;
import pri.ydy.educenter.mapper.UcenterMemberMapper;
import pri.ydy.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import pri.ydy.serviceBase.exceptionhandler.GuliException;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-12-22
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String ,String > redisTemplate;

    @Override//登录方法
    public String login(UcenterMember member) {
        String mobile=member.getMobile();
        String password=member.getPassword();
        //手机号和密码非空判断
        if(StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)){
            throw new GuliException(20001,"账号或密码为空，登录失败");
        }

        //判断数据库中手机号是否存在
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper);
        //判断手机号是否为空
        if (StringUtils.isEmpty(mobileMember)){
            throw new GuliException(20001,"登录失败(手机号不存在)");
        }
        //密码是否正确
        if (!MD5.encrypt(password).equals(mobileMember.getPassword())){
            throw new GuliException(20001,"登录失败（密码错误）");
        }
        //用户是否禁用
        if (mobileMember.getIsDisabled()){
            throw new GuliException(20001,"登录失败（用户禁用）");
        }
        //登录成功,根据规则jwt生成token字符串。
        String token= JwtUtils.getJwtToken(mobileMember.getId(),mobileMember.getNickname());
        System.out.println("1111111111111");
        System.out.println(token);
        return token;
    }

    //注册
    @Override
    public void register(RegisterVo registerVo) {
        //获取注册数据
        String code=registerVo.getCode();
        String mobile=registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();
        //判断非空
        if (StringUtils.isEmpty(code)||StringUtils.isEmpty(mobile)||StringUtils.isEmpty(nickname)||StringUtils.isEmpty(password)){
            throw new GuliException(20001,"注册失败（存在空元素）");
        }

        //判断验证码
        //获取Redis中的验证码
        String redisCode=redisTemplate.opsForValue().get(mobile);
        //TODO 修改验证码为redisCode
        if (!code.equals("1111")){
            throw new GuliException(20001,"注册失败（验证码错误）");
        }
        //手机号是否重复
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer integer = baseMapper.selectCount(wrapper);
        if (integer>0){
            throw new GuliException(20001,"注册失败（手机号已存在）");
        }

        //添加到数据库，注册成功
        UcenterMember member=new UcenterMember();
        member.setMobile(mobile);
        member.setPassword(MD5.encrypt(password));
        member.setNickname(nickname);
        member.setIsDisabled(false);//不禁用
        member.setAvatar("https://edu-ydy01.oss-cn-chengdu.aliyuncs.com/2020/12/19/654ee8b0b1844577af51101057cfa71dfile.png");//设置默认头像
        baseMapper.insert(member);
    }

    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    //查询某一天的注册人数
    @Override
    public Integer countRegister(String day) {
        return baseMapper.countRegister(day);
    }
}
